package com.dmtt.web.service.impl;

import com.dmtt.web.common.Constant;
import com.dmtt.web.entity.User;
import com.dmtt.web.mapper.UserMapper;
import com.dmtt.web.service.UserService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate t;

    public void saveOrder() throws Exception {
        String orderId = UUID.randomUUID().toString();
        //不在直接执行数据操作，而是改成向rabbitmq发送订单对象
        t.convertAndSend("orderQueue",orderId);

    }

    @Override
    public String login(User user, HttpServletRequest request) {
        User flag = userMapper.login(user);
        request.getSession().setAttribute(Constant.SESSION_USER,flag);

        if (flag != null) {
            switch (flag.getUserName()) {
                case "管理员":
                    return "管理员";
                case "观察者":
                    return "观察者";
                case "使用者":
                    return "使用者";
                default:
                    return "登陆失败";
            }
        }else {
            return "登陆失败";
        }
    }
}
